package com.freight.file.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.freight.core.common.exception.FreightException;
import com.freight.core.common.redis.RedisTemplateHelper;
import com.freight.core.common.utils.HttpUtils;
import com.freight.core.common.utils.ObjectUtil;
import com.freight.file.constants.FileConstant;
import com.freight.file.vo.BackFileVo;
import com.freight.file.vo.FaceFileVo;
import com.freight.file.vo.PicFileVo;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

import static com.freight.file.util.FileUtils.baiud_img_base64;
import static com.freight.file.util.BaiDuToken.getAuth;
/**
 * @author hytma
 */
public class DriverOCRUtil {


    //百度地址
    public static String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/driving_license";


    public static PicFileVo baiDuJustDriver(String imgBase64, RedisTemplateHelper redisTemplate) {

        HashMap<String, Object> result = new HashMap<>();
        FaceFileVo resO = null;

        try {
            String param = "driving_license_side=" + "front" + "&image=" + imgBase64;

            if(redisTemplate.get("baiduAccessToken")==null){
                getAuth(redisTemplate);
            }
            String accessToken = redisTemplate.get("baiduAccessToken");
            String res = HttpUtil.post(url, accessToken, param);
            System.out.println(res);

            JSONObject res_obj = JSON.parseObject(res);
            if(res_obj.containsKey("error_code")){
                throw new FreightException("图片不清楚，请重新上传");
            }
            JSONObject driverjson = res_obj.getJSONObject("words_result");


            result.put("num", driverjson.getJSONObject("证号").getString("words"));
            result.put("driverType", driverjson.getJSONObject("准驾车型").getString("words"));
            result.put("issueDate", driverjson.getJSONObject("初次领证日期").getString("words"));
            result.put("issuingOrganizations", driverjson.getJSONObject("发证单位").getString("words"));
            resO = (FaceFileVo) ObjectUtil.autoFillMapFields(result, FaceFileVo.class);

            System.out.println(result);
            return resO;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resO;
    }


    public static PicFileVo baiDuBackDriver(String imgBase64, RedisTemplateHelper redisTemplate) {
        // 请求url
        HashMap<String, Object> result = new HashMap<>();
        BackFileVo resO = null;
        try {
            String param = "driving_license_side=" + "back" + "&image=" + imgBase64;

            if(redisTemplate.get("baiduAccessToken")==null){
                getAuth(redisTemplate);
            }
            String accessToken = redisTemplate.get("baiduAccessToken");
            String res = HttpUtil.post(url, accessToken, param);
            System.out.println(res);

            JSONObject res_obj = JSON.parseObject(res);
            if(res_obj.containsKey("error_code")){
                throw new FreightException("图片不清楚，请重新上传");
            }
            JSONObject idcardjson = res_obj.getJSONObject("words_result");

            result.put("num", idcardjson.getJSONObject("证号").getString("words"));
            result.put("name", idcardjson.getJSONObject("姓名").getString("words"));
            result.put("archiveNo",idcardjson.getJSONObject("档案编号").getString("words"));
            System.out.println(result);
            resO = (BackFileVo) ObjectUtil.autoFillMapFields(result, BackFileVo.class);

            return null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
